package com.geekaca.d4.d6utils;

import java.util.Arrays;
import java.util.Comparator;

public class TestArraysDemo2 {
    public static void main(String[] args) {
        Integer[] ages = {34,12,42,23};

        Arrays.sort(ages, (o1, o2) -> o1-o2); // 用lambda表达式替换 比较复杂逻辑

        System.out.println(Arrays.toString(ages));

        System.out.println("============================");
        testSortCustom();
    }

    public static void testSortCustom(){
        Student[] stuArr = new Student[5];
        for (int i = 0; i < stuArr.length; i++) {
            stuArr[i] = new Student("Tom"+i, 19+i,175+i);
        }
        System.out.println(Arrays.toString(stuArr));

        // 依照年龄排序
        Arrays.sort(stuArr, new Comparator<Student>() {
            @Override
            public int compare(Student stu1, Student stu2) {
                return stu2.getAge()-stu1.getAge();
            }
        });

        System.out.println(Arrays.toString(stuArr));
    }
}
